﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Facebook.Service.Core;
using FriendsNearby.Services;
using System.Collections.Generic;
using FriendsNearby.Services.Entities;
using FriendsNearby.Services.Facebook;
using Facebook.Service;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace FriendsNearby.Fbml
{
    public partial class RequestIntroduction : System.Web.UI.Page
    {
        string friendOfFriendFacebookUserID;
        FriendsNearbySystem friendsNearbySystem;
        string nameOfMutualFriendIfSingleAndNonHidden = string.Empty;
        IUsersService userService;
        ISocialNetworkService socialNetworkService;

        protected void Page_Load(object sender, EventArgs e)
        {
            friendsNearbySystem = new FriendsNearbySystem();
            userService = friendsNearbySystem.GetService<IUsersService>();
            socialNetworkService = friendsNearbySystem.GetService<ISocialNetworkService>();
            
            if (!IsPostBack)
            {
                FriendOfFriend.Value = Request["target_friendoffriend"];
            }

            friendOfFriendFacebookUserID = userService.GetFacebookUserIDFromInternalUserID(FriendOfFriend.Value);
            AnonymousMutualText.Visible = !CanShowMutualFriends(friendOfFriendFacebookUserID);

            if (!IsPostBack)
            {
                List<string> mutualFriends = new List<string>(socialNetworkService.GetFriendsInCommon(fbApplication.UserID, friendOfFriendFacebookUserID));

                if (mutualFriends.Count == 0)
                {
                    RequestFormElements.Visible = false;
                    RequestSucceded.Visible = false;
                    RequestNoCommonFriends.Visible = true;

                    LogEntry logEntry = new LogEntry();
                    logEntry.Message = string.Format("Somehow no mutual friends can be found within the Request introduction dialog. {0} Requesting an introduction to {1}", fbApplication.UserID, friendOfFriendFacebookUserID);
                    logEntry.Categories.Add("Oddities");
                    logEntry.EventId = LogEventIds.NoMutualFriensInRequestIntroductionDialog;
                    Logger.Write(logEntry);
                }

                if (CanShowMutualFriends(friendOfFriendFacebookUserID))
                {
                    if (mutualFriends.Count == 1)
                    {
                        MutualFriendListContainer.Visible = false;
                        MutualFriend.Value = userService.GetInternalUserIDFromFacebookUserID(mutualFriends[0]);
                        nameOfMutualFriendIfSingleAndNonHidden = socialNetworkService.GetUserDisplayName(fbApplication.UserID, mutualFriends[0]);
                    }
                    else
                    {
                        MutualFriendListContainer.Visible = true;

                        foreach (string mutualFriendFacebookID in mutualFriends)
                        {
                            string displayName = socialNetworkService.GetUserDisplayName(fbApplication.UserID, mutualFriendFacebookID);
                            string mutualFriendInternalID = userService.GetInternalUserIDFromFacebookUserID(mutualFriendFacebookID);

                            MutualFriendList.Items.Add(new ListItem(displayName, mutualFriendInternalID));
                        }
                    }
                }
                else
                {
                    MutualFriendListContainer.Visible = false;
                }
            }
            if (MutualFriendList.SelectedItem != null) // a selection was made in the dropdown. normalize to hidden field.
            {
                MutualFriend.Value = MutualFriendList.SelectedValue;
            }

        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(MutualFriend.Value))
            {
                string mutualFriendFacebookID = userService.GetFacebookUserIDFromInternalUserID(MutualFriend.Value);
                socialNetworkService.SendIntrodutionNotification(FriendRequestMessage.Text, fbApplication.UserID, friendOfFriendFacebookUserID, mutualFriendFacebookID);
            }
            else
            {
                foreach (string mutualFriend in socialNetworkService.GetFriendsInCommon(fbApplication.UserID, friendOfFriendFacebookUserID))
                {
                    socialNetworkService.SendIntrodutionNotification(FriendRequestMessage.Text, fbApplication.UserID, friendOfFriendFacebookUserID, mutualFriend);
                }
            }
            RequestFormElements.Visible = false;
            RequestSucceded.Visible = true;
        }

        private bool CanShowMutualFriends(string friendOfFriendFacebookUserID)
        {
            UserPreference friendOfFriendPreference = userService.GetUserPreferences(friendOfFriendFacebookUserID);
            return (friendOfFriendPreference.VisibilityLevel == (int)VisibilityLevel.Everyone || friendOfFriendPreference.VisibilityLevel == (int)VisibilityLevel.ContactsAndTheirContacts);
        }

        protected string GetMutualFriendNameIfOnlyOneExistsAndCanShow()
        {
            if (!String.IsNullOrEmpty(nameOfMutualFriendIfSingleAndNonHidden))
            {
                return String.Format(" ({0})", nameOfMutualFriendIfSingleAndNonHidden);
            }
            return string.Empty;
        }

    }
}
